package com.appology.chuck;

import java.io.IOException;
import java.io.PrintWriter;

import javax.jdo.PersistenceManagerFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.appology.oauth2.OAuth2ClientCredentials;
import com.appology.oauth2.PMF;
import com.google.api.client.extensions.appengine.auth.AbstractAppEngineFlowServlet;
import com.google.api.client.extensions.auth.helpers.Credential;
import com.google.api.client.extensions.auth.helpers.ThreeLeggedFlow;
import com.google.api.client.googleapis.extensions.auth.helpers.oauth2.draft10.GoogleOAuth2ThreeLeggedFlow;
import com.google.api.services.buzz.Buzz;
import com.google.api.services.buzz.Buzz.People;
import com.google.api.services.buzz.Buzz.People.Get;
import com.google.api.services.buzz.model.Activity;
import com.google.api.services.buzz.model.ActivityObject;
import com.google.api.services.buzz.model.ActivityObjectLinks;
import com.google.api.services.buzz.model.Person;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class PostActivity extends AbstractAppEngineFlowServlet{

	@Override
	  protected ThreeLeggedFlow newFlow(String userId) {
		
	    return new GoogleOAuth2ThreeLeggedFlow(userId,
	        OAuth2ClientCredentials.CLIENT_ID,
	        OAuth2ClientCredentials.CLIENT_SECRET,
	        OAuth2ClientCredentials.SCOPE,
	        "http://appology-chuck-norris.appspot.com/oauth2callback");
	  }

	  @Override
	  protected void doGetWithCredentials(
	      HttpServletRequest req, HttpServletResponse resp, Credential credential) throws IOException {
		  UserService userService = UserServiceFactory.getUserService();
		  String thisURL = req.getRequestURI();
		  resp.setContentType("text/plain");
		  PrintWriter writer = resp.getWriter();
		  String id = null;
		  if (req.getUserPrincipal() != null) {          
			  id = userService.getCurrentUser().getUserId();
//	          writer.println(userService.getCurrentUser().getUserId());   
	      }
		  if(req.getParameter("joke") != null){
			    String joke = req.getParameter("joke");
			    // List activities using the generated client
			    Buzz buzz = new Buzz(getHttpTransport(), credential, getJsonFactory());
			    People people = buzz.people;
			    Get get = people.get("@me");
			    Person person = get.execute();
			    
		//	    writer.println(id);
			    String name = (String) person.displayName;
			    
			    Activity activity = new Activity();
			    activity.buzzObject = new ActivityObject();
			    activity.buzzObject.content = joke + "\n(http://appology-chuck-norris.appspot.com)";
			    Activity result = buzz.activities.insert("@me", activity).execute();
			    
			    String url = (String) person.profileUrl + "/buzz";
			    
			    
			    if(!result.isEmpty()){
			    	writer.println(url);
			    } else {
			    	writer.println("failed");
			    }
		  } else {
			  writer.println("No joke was submitted");
		  }
	 
//	    ActivityFeed activities = buzz.activities.list("me", "@self").execute();

//	    writer.println(Joke.getInstance().getRandom(name));
//	    String destination  ="/index.jsp";        
//	    resp.sendRedirect(resp.encodeRedirectURL(destination));
//	    writer.println("Stream for: " + name);
//	    writer.println("Activities:");
//	    for (Activity oneAct : activities.items) {
//	      writer.println(oneAct.title);
//	    }
	  }
	
	  @Override
	  protected PersistenceManagerFactory getPersistenceManagerFactory() {
	    return PMF.get();
	  }

}
